package com.example.sample.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.app.BaseActivity;

import net.liteheaven.xblib.common.ui.ToastUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import androidx.appcompat.app.AlertDialog;

import static android.content.DialogInterface.BUTTON_NEGATIVE;
import static android.content.DialogInterface.BUTTON_POSITIVE;

/**
 * Created by LiangYu on 2017/10/26.
 */
public class ThreadActivity extends BaseActivity {

    private MyThread thread;
    private ExecutorService es = Executors.newScheduledThreadPool(10);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        Button b1 = new Button(this);
        b1.setText("开启线程");
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (thread == null){
                    thread = new MyThread();
                    thread.start();
                }else{
                    ToastUtil.show(ThreadActivity.this, "已开启");
                }
            }
        });
        Button b2 = new Button(this);
        b2.setText("停止线程");
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (thread != null){
                    thread.safeStop();
                    thread = null;
                }else{
                    ToastUtil.show(ThreadActivity.this, "未开启");
                }
            }
        });
        ll.addView(b1);
        ll.addView(b2);
        Button b3 = new Button(this);
        b3.setText("在线程池开启");
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                es.submit(r);
            }
        });
        Button b4 = new Button(this);
        b4.setText("在线程池停止");
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                es.shutdown();
            }
        });
        ll.addView(b3);
        ll.addView(b4);
        setContentView(ll);

        AlertDialog d = new AlertDialog.Builder(this)
                .create();
                d.setTitle("哈哈");
                d.setButton(BUTTON_POSITIVE, "ok", (DialogInterface.OnClickListener) null);
        d.setButton(BUTTON_NEGATIVE, "ok", (DialogInterface.OnClickListener) null);
        d.show();
    }

    private class MyThread extends Thread{

        private boolean running = true;
        private Object lock = new Object();

        public MyThread(){

        }

        public void safeStop(){
            running = false;
            synchronized (lock){
                lock.notifyAll();
            }
        }

        @Override
        public void run() {

            log("线程开始成功");

            while (running){
                try{
                    synchronized(lock) {
                        lock.wait();
                    }
                }catch (InterruptedException e){
                    ;
                }
            }

            log("线程停止成功");
        }
    }

    public void log(String str){
        Log.i("ThreadActivity", str);
    }

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            Thread.currentThread().setName("握草");
            try{
                Thread.sleep(1*1000);
            }catch (Exception e){

            }
        }
    };
}
